//
//  YuleCollectionViewCell.m
//  news
//
//  Created by imacbookair on 16/3/12.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "YuleCollectionViewCell.h"
#import "QiCheOneTableViewCell.h"
#import "QiCheThreeTableViewCell.h"
#import "QiCheTwoTableViewCell.h"
@interface YuleCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation YuleCollectionViewCell

#pragma mark - 初始化

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        [self headerData];
        
        [self.contentView addSubview:_tableView];
        [_tableView NightWithType:UIViewColorTypeNormal];
        
    }
    return self;
}

#pragma mark - 布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 155 * KWITH, 0);
    // 注册重用池
    [_tableView registerClass:[QiCheOneTableViewCell class] forCellReuseIdentifier:@"QiCheOneTableViewCell"];
    [_tableView registerClass:[QiCheTwoTableViewCell class] forCellReuseIdentifier:@"QiCheTwoTableViewCell"];
    [_tableView registerClass:[QiCheThreeTableViewCell class] forCellReuseIdentifier:@"QiCheThreeTableViewCell"];

 
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [_tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
    
}

#pragma mark - 刷新方法
- (void)headerData
{
    NSString *urlStr = @"http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html";
    [AFNetBlock GetDataWithURL:urlStr block:^(id block) {
        self.dataArr = [YuleModel modelHanderWithArray:block[@"T1348648517839"]];
        [_tableView reloadData];
    }];
}

#pragma mark - 加载方法
- (void)footerData
{
    static NSInteger t = 20;
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348648517839/%ld-20.html", (long)t];
    
    [AFNetBlock GetDataWithURL:str block:^(id block) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [YuleModel modelHanderWithArray:block[@"T1348648517839"]];
        for (NSInteger i = 0; i < arrTemp.count; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    t += 20;
}

#pragma mark - tableView的协议方法实现

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YuleModel *y = _dataArr[indexPath.row];
    NSMutableArray *arr = [y.imgextra mutableCopy];
    if (indexPath.row == 0) {
        QiCheOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QiCheOneTableViewCell" forIndexPath:indexPath];
        [cell.daTu sd_setImageWithURL:[NSURL URLWithString:y.imgsrc]zhanweitu];
        cell.biaoti.text = y.title;
        
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }else if (arr.count == 0){
        QiCheTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QiCheTwoTableViewCell" forIndexPath:indexPath];
        [cell.tu sd_setImageWithURL:[NSURL URLWithString:y.imgsrc]zhanweitu];
        cell.title.text = y.title;
        
        cell.neirong.text = y.digest;
        
        cell.pin.text = [NSString stringWithFormat:@"%@ 跟帖", y.replyCount.stringValue];
        
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }else{
        QiCheThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QiCheThreeTableViewCell" forIndexPath:indexPath];
        cell.title.text = y.title;
        
        [cell.zuo sd_setImageWithURL:[NSURL URLWithString:y.imgsrc]zhanweitu];
        [cell.zhong sd_setImageWithURL:[NSURL URLWithString:arr[0][@"imgsrc"]]zhanweitu];
        [cell.you sd_setImageWithURL:[NSURL URLWithString:arr[1][@"imgsrc"]]zhanweitu];
        cell.pin.text = [NSString stringWithFormat:@"%@ 跟帖", y.replyCount.stringValue];
        
        [cell NightWithType:UIViewColorTypeNormal];
        
        return cell;
    }
}

// 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YuleModel *y = _dataArr[indexPath.row];
    NSMutableArray *arr = [y.imgextra mutableCopy];
    if (indexPath.row == 0) {
        return 180 * KHIGHT;
    }else if (arr.count == 0){
        return 90 * KHIGHT;
    }else{
        return 140 * KHIGHT;
    }
}

// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YuleModel *y = _dataArr[indexPath.row];
    [self.delegate DadaValue:y.url];
}
@end
